﻿layui.use(['form'], function () {
    let form = layui.form;
    form.render();

    initForm(form)

    /* 提交 */
    form.on('submit(saveTableInfo)', function (data) {
        let obj = data.field;
        $.form.submit({
            url: HTTP_BIZ_URI + '/dev/table/info',
            data: obj,
            callback: function (res) {
                layer.alert(res.msg, {title: "系统提示", icon: 1}, function () {
                    //  goBackPreviousPage();
                    location.href = HTTP_BIZ_URI + '/dev/table/info/index'
                });
            }
        })
    });

});


function initForm(form) {
    let tpl = ['biz', 'default'];
    let tplCategory = 'biz';
    let html = '';
    for (let i = 0; i < tpl.length; i++) {
        html = '<input type="radio" name="rearEndTemp" value="' + tpl[i] + '" title="' + tpl[i] + '"';
        if (tplCategory === tpl[i]) {
            html += 'checked=""'
        }
        html += '>'
        $("#tplCategoryBlock").append(html)
    }


    initBuilderBlock('rearDddBlock', 'rearDdd', '1');
    initBuilderBlock('buildEntityBlock', 'buildEntity', '1');
    initBuilderBlock('buildQueryBlock', 'buildQuery', '1');
    initBuilderBlock('buildDtoBlock', 'buildDto', '1');
    initBuilderBlock('buildVoBlock', 'buildVo', '1');
    initBuilderBlock('rearSplitBlock', 'rearSplit', '1');


    selectUiStyle('LAY_UI');
    browserOpeningIframe('iframe');


    form.render();
}


function initBuilderBlock(id, name, value) {
    let yesOrNo = ['1', '0'];
    let html = '';
    for (let i = 0; i < yesOrNo.length; i++) {
        html = '<input type="radio" id="' + name + '" name="' + name + '"  value="' + yesOrNo[i] + '" ';
        if ('1' === yesOrNo[i] || 1 === yesOrNo[i]) {
            html += ' title="是" ';
        } else {
            html += ' title="否" ';
        }
        if (value === yesOrNo[i]) {
            html += 'checked=""'
        }
        html += '>'
        $("#" + id).append(html)
    }
}

/**
 * 浏览器打开方式
 * @param iframe
 */
function browserOpeningIframe(iframe) {
    let iframeArr = [{
        value: 'iframe',
        label: 'iframe(内嵌)'
    }, {
        value: '_parent',
        label: '_parent(父窗口打开)'
    }, {
        value: '_blank',
        label: '_blank(新窗口打开)'
    }];
    let html = '<option value="">请选择浏览器打开方式</option>';
    for (let i = 0; i < iframeArr.length; i++) {
        if (iframe === iframeArr[i].value) {
            html += '<option value="' + iframeArr[i].value + '"  selected>' + iframeArr[i].label + '</option>';
        } else {
            html += '<option value="' + iframeArr[i].value + '">' + iframeArr[i].label + '</option>';
        }
    }
    $("#iframe").append(html)
}

/**
 * UI 类型
 * @param iframe
 */
function selectUiStyle(uiStyle) {
    let uiStyleArr = [{
        value: 'LAY_UI',
        label: 'layUI (JQ)'
    }, {
        value: 'VUE_ELEMENT',
        label: 'layUI ( VUE3/TS )'
    }, {
        value: 'VUE_LAY_UI',
        label: 'Element UI Plus ( VUE3/TS )'
    }];
    let html = '<option value="">请选择 UI 类型</option>';
    for (let i = 0; i < uiStyleArr.length; i++) {
        if (uiStyle === uiStyleArr[i].value) {
            html += '<option value="' + uiStyleArr[i].value + '"  selected>' + uiStyleArr[i].label + '</option>';
        } else {
            html += '<option value="' + uiStyleArr[i].value + '">' + uiStyleArr[i].label + '</option>';
        }
    }
    $("#frontUiStyle").append(html)
}